'''
Created on 2017年12月18日

@author: Administrator

#如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Large Clusters”，
你就能大概明白map/reduce的概念。
##
'''
from _functools import reduce
from builtins import map

def f(x):
    return pow(x, 2)
print(f(2))

r = map(f,[1,2,3,5,6])

print(list(r))

#将数字列表转换为字符列表
print(list(map(str,[1,2,3])))

#reduce 函数把一个函数作用在一个序列上，这个函数两个入参，reduce把结果和序列的下一个元素做累积计算
def add(x,y):
    return x + y

x = reduce(add,[1,2,34,4])

print("家的结果：",x)


#字符串转成整数
def fn(x,y):
    return x * 10 + y

def getNumListStr(s):
    dicts = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
    return dicts[s]

a = '789'
print(reduce(fn,list(map(getNumListStr,a))))


#规范用户输入的名字
#大写变小写
def uppertolow(c):
    numc = ord(c)
    if numc < 97:
        numc = numc + 32
    return chr(numc)


def normalize(s):
    def appenChar(x,y):
        return x + y
    s = reduce(appenChar,map(uppertolow,s))
    numc = ord(s[0])
    s = chr(numc - 32) + s[1:]
    return s

def listStrUpper(l):
    return map(normalize,l)

# 测试:
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)
print(list(listStrUpper(L1)))
        

